package com.hp.shiro.service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hp.shiro.model.LogInfo;
import com.hp.shiro.repository.LogInfoRepository;

@Service
public class LogInfoService {

	@Autowired
	private LogInfoRepository logInfoRepository;

	public List<LogInfo> queryLogInfo(String account, String startDate, String endDate) {
		Date st = null;
		Date et = null;
		try {
			st = DateUtils.parseDate(startDate, "yyyy-MM-dd");
			et = DateUtils.parseDate(endDate, "yyyy-MM-dd");
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("时间格式错误");
		}
		if(StringUtils.isNotBlank(account)) {
			account = String.format("%s%s%s", "%",account,"%");
			return logInfoRepository.findByAccountLikeAndOperateTimeBetween(account, st, et);
		}else {
			return logInfoRepository.findByOperateTimeBetween(st, et);
		}
	}
	
	public LogInfo saveLogInfo(LogInfo info) {
		return logInfoRepository.save(info);
	}
	
	
}
